﻿<cfscript>
/** 
* ORM CourseBook
* 
* @hint "Hibernate persistenter t_course_book. 学期教材选用目录" 
* 
*/ 

component 
	displayname="public.service.orm.senate.CourseBook" 
	output=false 
	persistent=true 
	selectBeforeUpdate=true dynamicInsert=true dynamicUpdate=true 
	cacheuse="Transactional" 
	entityname="CourseBook" 
	table="t_course_book"
	{
	
	property type="string" name="CourseBookID" column="cbid" fieldtype="id" generator="assigned" hint="教材选用编号";

	property type="string" name="BookIndex" column="bidx" hint="课程教材序列";

	property type="numeric" name="Prise" column="prise" hint="定价";
	property type="numeric" name="Discount" column="discount" hint="折扣率";
	property type="numeric" name="Charge" column="charge" hint="实际售价";
	property type="numeric" name="PlanCounts" column="plan_sum" hint="计划采购量";
	property type="numeric" name="ForStudent" column="plan_student" hint="学生用书量";
	property type="numeric" name="ForTeacher" column="plan_teacher" hint="教师用书量";

	property type="string" name="Contract" column="contract" hint="联系人";
	property type="string" name="Telephone" column="telephone" hint="联系电话";
		
	property name="Term" fieldtype="many-to-one" fkcolumn="term_id" cfc="Term"  hint="学期";
	property name="Course" fieldtype="many-to-one" fkcolumn="cid" cfc="Course"  hint="课程";
	property name="Book" fieldtype="many-to-one" fkcolumn="bid" cfc="Book"  hint="教材";

	property name="StudentBooks" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="cbid" cfc="StudentBook" hint="学生教材领用情况";
	property name="FormClassBooks" type="array" lazy="true" inverse="true" cascade="delete-orphan" fieldtype="one-to-many" fkcolumn="cbid" cfc="FormClassBook" hint="班级教材征订计划";
	
	/**
	* @hint "生成课程教材序列"
	* 
	* @Term "学期"
	* @Course "课程"
	*/
	public string function getBookIndex( required Term, required Course ) output=false {
		/* 
			课程教材序列规则 1位代码
		*/
		var nextID = "0";
		
		var checkArray = ORMExecuteQuery("select BookIndex from CourseBook 
											where 
											Term = :term and Course = :course
											order by BookIndex desc", {term=arguments.Term, course=arguments.Course}, { maxResults=1 });
		
		if (arrayLen(checkArray)) {
			/* 创建一个追加id */
			nextID = checkArray[1] + 1;
		}

		return nextID;
		
	}
	
	/**
	* @hint "自定义主键规则"
	* 
	* @Term "学期"
	* @Course "课程"
	* @BookIndex "课程教材序列"
	*/
	public string function getID( required Term, required Course, required string BookIndex ) output=false {
		
		var nextID = arguments.Term.getTermID() & "," & arguments.Course.getCourseID() & "," & arguments.BookIndex;

		/* 
			主键生成规则 
			
			教材选用编号12位 3位学期代码, 6位本科课程号, 1位课程教材序列

			Unique: TERM_ID, CID, BIDX
		*/
		return nextID;
	}
	
}

</cfscript>